package zqkcom.atguigu.service.impl;

import zqkcom.atguigu.bean.Book;
import zqkcom.atguigu.bean.Page;
import zqkcom.atguigu.dao.BookDao;
import zqkcom.atguigu.dao.impl.BookDaoImpl;
import zqkcom.atguigu.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {

     private BookDao bookDao = new BookDaoImpl();

    @Override
    public Page<Book> getPageByPrice(Page<Book> page, Integer minPrice, Integer maxPrice) {
        //获取总记录数totalCount，通过totalCount和pageSize获取总页数totalPage
        Integer totalCount = bookDao.getCountByPrice(minPrice, maxPrice);
        page.setTotalCount(totalCount);
        //通过index和pageSize获取分页数据list
        List<Book> list = bookDao.getBookPageByPrice(page, minPrice, maxPrice);
        page.setList(list);
        return page;
    }

    @Override
    public Page<Book> getPage(Page<Book> page) {
        /**
         * 此方法是用来完善page对象的，其实就是获取所有分页相关数据的过程
         * 1、pageNoStr是从浏览器获取当前页的页码，因此page对象中pageNoStr已经进行了赋值
         * 2、获取总记录数totalCount，通过totalCount和pageSize获取总页数totalPage
         * 3、通过pageNoStr和totalPage获取当前页pageNo
         * 4、通过pageNo和pageSize获取index
         * 5、通过index和pageSize获取分页数据list
         */
        //获取总记录数totalCount，通过totalCount和pageSize获取总页数totalPage
        Integer totalCount = bookDao.getCount();
        page.setTotalCount(totalCount);
        //通过index和pageSize获取分页数据list
        List<Book> list = bookDao.getBookPage(page);
        page.setList(list);
        return page;


    }





    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBook(Integer id) {
        bookDao.deleteBook(id);
    }

    @Override
    public Book getBookByBookId(Integer id) {
        return bookDao.getBookByBookId(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }
}
